<?php
$application = $response->getAttribute("application");

$object = $response->getAttribute("object");
$objectType = $response->getAttribute("objectType");
$typeId = $response->getAttribute("typeId");

$children = null ;
if ($object!=null) $children = $object->getChildren();

$types = null ;
if ($typeId == null) {
    $typeDao = DAOFactory::getDAO("Type");
    $types = $typeDao->retrieveBaseTypes();
}
?>
<script type="text/javascript">

    var subTypes = new Array();
    
    <?php    
    if ($children != null) {
        $i =0 ;
        foreach ($children as $child) {
            if ($child != null) {
                echo "subTypes[".$i."] = getType('".$child->getId()."', '".$child->getNom()."');\n" ;
                $i++ ;
            }
        }
    } else {
        echo "subTypes[0] = getEmptyType();\n" ;
    }
    ?>

    var subTypeIndex = subTypes.length ;
</script>
<form
    class="editForm"
    method="POST"
    action="<?php Printer::display(HTML::linkHref('action=CMS.Save&type'.$objectType));?>">
    <input
        type="hidden"
        name="id"
        value="<?php if ($object != null) Printer::display($object->getId()); ?>" />
    <span class="label">
        <span class="titre">Nom :</span>
        <input
            class="firstInput"
            type="text"
            name="nom"
            value="<?php if ($object != null) Printer::display($object->getNom()); ?>" />
    </span>
    <?php 
    $selectedId = null ;
    if ($object != null && $object->getParent() != null) {
        $selectedId = $object->getParent()->getId();
    }
    $response->setAttribute("selectedId", $selectedId);
    $response->setAttribute("types", $types);
    $response->setAttribute("inputName", "parent_type_id");
    $response->setAttribute("title", "Type parent :");
    TemplateManager::display($application, "main/cms/edit/elements/typeSelectList", $response)
    ?>
    <span class="label" id="subTypesSpan">
        <span class="titre">Sous-types :</span>
        <input
            type="button"
            name="addSubType"
            value="+"
            onclick="javascript:addNewSubType(this.parentNode, subTypeIndex); subTypeIndex++;"
            class="firstInput" />
        <script type="text/javascript">
            for(var i=0; i<subTypeIndex; i++) {
                addSubType(document.getElementById('subTypesSpan'), subTypes[i], i);
            }
        </script>
    </span>
    <input type="submit" value="OK" />
</form>
